package com.jdaw.datastructure.tree.threadedbinarytree;

public class ThreadedBinaryTreeDemo {
    public static void main(String[] args) {
        //测试一把中序线索二叉树的功能
        HeroNode root = new HeroNode(1, "tom");
        HeroNode node2 = new HeroNode(3, "jack");
        HeroNode node3 = new HeroNode(6, "smith");
        HeroNode node4 = new HeroNode(8, "mary");
        HeroNode node5 = new HeroNode(10, "king");
        HeroNode node6 = new HeroNode(14, "dim");

        //二叉树，后面我们要递归创建, 现在简单处理使用手动创建
        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);

        ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree();
        threadedBinaryTree.setRoot(root);
        threadedBinaryTree.threadedNodes();

//        HeroNode leftNode = node5.getLeft();
//        HeroNode rightNode = node5.getRight();
//        System.out.println("10号结点的前驱结点是 ="  + leftNode); //3
//        System.out.println("10号结点的后继结点是="  + rightNode); //1
//        System.out.println("使用线索化的方式遍历 线索化二叉树");
//        threadedBinaryTree.threadedList(); // 8, 3, 10, 1, 14, 6
    }
}

class ThreadedBinaryTree{
    private HeroNode root;
    //创建一个指向当前节点的前驱结点的指针
    private HeroNode pre =null;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    //重载一把threadedNodes方法
    public void threadedNodes() {
        this.threadedNodes(root);
    }

    //遍历线索化二叉树的方法
    public void threadedList(){
        //定义一个变量储存当前遍历的节点
        HeroNode node=root;
        while(node!=null){
            //找到第一个节点，就是左结点为空的
            while(node.getLeftType()!=1){
                node=node.getLeft();
            }
            System.out.println(node);
            while(node.getRightType()==1){
                node=node.getRight();
                System.out.println(node);
            }
            node=node.getRight();
        }
    }

    //对二叉树进行中序线索化
    public void threadedNodes(HeroNode node){
        System.out.println(node);
        if(node==null){
            return;
        }

        threadedNodes(node.getLeft());
        if(node.getLeft()==null){
            node.setLeft(pre);
            node.setLeftType(1);
        }
        if(pre!=null&&pre.getRight()==null){
            pre.setRight(node);
            pre.setRightType(1);
        }
        //让当前结点变为下一个结点的前驱结点
        pre=node;
        threadedNodes(node.getRight());

    }

    //删除节点
    public void delNode(int no){
        if(root==null){
            System.out.println("空树不能删除");
        }else{
            if(root.getNo()==no){
                root=null;
            }else{
                root.delNode(no);
            }
        }
    }
    //前序遍历
    public void preOrder(){
        if(this.root!=null){
            this.root.preOrder();
        }
    }
    //中序遍历
    public void midOrder(){
        if(this.root!=null){
            this.root.midOrder();
        }
    }
    //后序遍历
    public void postOrder(){
        if(this.root!=null){
            this.root.postOrder();
        }
    }

    //前序查找
    public HeroNode preSearch(int no){
        if (root != null) {
            return root.preSearch(no);
        } else{
            return null;
        }
    }
    //中序查找
    public HeroNode midSearch(int no){
        if (root != null) {
            return root.midSearch(no);
        } else{
            return null;
        }
    }
    //后序查找
    public HeroNode postSearch(int no){
        if (root != null) {
            return root.postSearch(no);
        } else{
            return null;
        }
    }
}

class HeroNode{
    private String name;
    private int no;
    private HeroNode left;
    private HeroNode right;

    //若leftType=0表示为指向左子树，1为指向前驱结点
    private int leftType;
    private int rightType;

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    public HeroNode( int no,String name) {
        this.name = name;
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "name='" + name + '\'' +
                ", no=" + no +
                '}';
    }

    //删除节点
    public void delNode(int no){
        if(this.left!=null&&this.left.no==no){
            this.left=null;
            return;
        }
        if(this.right!=null&&this.right.no==no){
            this.right=null;
            return;
        }
        if(this.left!=null){
            this.left.delNode(no);
        }
        if(this.right!=null){
            this.right.delNode(no);
        }
    }

    //前序遍历
    public void preOrder(){
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }
    //前序查找
    public HeroNode preSearch(int no){

        if(this.no==no){
            return this;
        }
        HeroNode resNode=null;
        if (this.left != null) {
            resNode=this.left.preSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if (this.right != null) {
            resNode=this.right.preSearch(no);
        }
        return resNode;
    }
    //中序遍历
    public void midOrder(){
        if (this.left != null) {
            this.left.midOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.midOrder();
        }
    }
    //中序查找
    public HeroNode midSearch(int no){
        HeroNode resNode=null;
        if (this.left != null) {
            resNode=this.left.midSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if(no==this.no){
            return this;
        }
        if (this.right != null) {
            resNode=this.right.midSearch(no);
        }
        return resNode;
    }
    //后序遍历
    public void postOrder(){
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
        System.out.println(this);
    }
    //后序查找
    public HeroNode postSearch(int no){
        HeroNode resNode=null;
        if (this.left != null) {
            resNode=this.left.postSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if (this.right != null) {
            resNode=this.right.postSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if(this.no==no){
            return this;
        }
        return resNode;
    }
}